package com.zdb.java.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ShareData {
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void add() {
        lock.lock();
        try {
            System.out.println("------ add --------");
            TimeUnit.SECONDS.sleep(6);
            System.out.println("------ sleep over ------");
            condition.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            System.out.println("----- unlock ------");
            lock.unlock();
        }


    }

    public void del() {
        condition.signal();
    }

    public void interrupt() {
        System.out.println("-----interrupt---");
        Thread.currentThread().interrupt();
    }


}


public class LockDemo {

    public static void main(String[] args) throws Exception {

        ShareData shareData = new ShareData();

        Thread t = new Thread(() -> {
//            shareData.add();
            shareData.del();
        }, "A");
        t.start();

        TimeUnit.SECONDS.sleep(3L);
//        t.interrupt();

        TimeUnit.SECONDS.sleep(1000L);
        System.out.println("---------------- over ---------");
    }
}
